package com.ocm.tourism.web;

import com.ocm.tourism.core.domain.CommonItems;
import com.ocm.tourism.core.domain.ItemTypes;
import com.ocm.tourism.core.service.CommonItemsService;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.util.UriUtils;
import org.springframework.web.util.WebUtils;

@RequestMapping("/commonitemses")
@Controller
public class CommonItemsController {

   @Autowired
   CommonItemsService commonItemsService;

   @RequestMapping(value = "/create", method = RequestMethod.GET)
   public String createForm(Model uiModel) {
      populateEditForm(uiModel, new CommonItems());
      return "commonitemses/create";
   }

   @RequestMapping(value = "/{id}", method = RequestMethod.GET)
   public String show(@PathVariable("id") Long id, Model uiModel) {
      uiModel.addAttribute("commonitems", commonItemsService.findCommonItems(id));
      uiModel.addAttribute("itemId", id);
      return "commonitemses/show";
   }

   @RequestMapping(value = "/{id}/update", method = RequestMethod.GET)
   public String updateForm(@PathVariable("id") Long id, Model uiModel) {
      populateEditForm(uiModel, commonItemsService.findCommonItems(id));
      return "commonitemses/update";
   }

   @RequestMapping(produces = "text/html")
   public String list(@RequestParam(value = "page", required = false) Integer page,
         @RequestParam(value = "size", required = false) Integer size, Model uiModel) {
      if (page != null || size != null) {
         int sizeNo = size == null ? 10 : size.intValue();
         final int firstResult = page == null ? 0 : (page.intValue() - 1) * sizeNo;
         uiModel.addAttribute("commonitemses", commonItemsService.findCommonItemsEntries(firstResult, sizeNo));
         float nrOfPages = (float) commonItemsService.countAllCommonItemses() / sizeNo;
         uiModel.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1
               : nrOfPages));
      } else {
         uiModel.addAttribute("commonitemses", commonItemsService.findAllCommonItemses());
      }
      return "commonitemses/list";
   }

   @RequestMapping(method = RequestMethod.POST, produces = "text/html")
   public String create(@Valid CommonItems commonItems, BindingResult bindingResult, Model uiModel,
         HttpServletRequest httpServletRequest) {
      if (bindingResult.hasErrors()) {
         populateEditForm(uiModel, commonItems);
         return "commonitemses/create";
      }
      uiModel.asMap().clear();
      commonItemsService.saveCommonItems(commonItems);
      return "redirect:/commonitemses/" + encodeUrlPathSegment(commonItems.getId().toString(), httpServletRequest);
   }

   @RequestMapping(method = RequestMethod.PUT, produces = "text/html")
   public String update(@Valid CommonItems commonItems, BindingResult bindingResult, Model uiModel,
         HttpServletRequest httpServletRequest) {
      if (bindingResult.hasErrors()) {
         populateEditForm(uiModel, commonItems);
         return "commonitemses/update";
      }
      uiModel.asMap().clear();
      commonItemsService.updateCommonItems(commonItems);
      return "redirect:/commonitemses/" + encodeUrlPathSegment(commonItems.getId().toString(), httpServletRequest);
   }

   @RequestMapping(value = "/{id}", method = RequestMethod.DELETE, produces = "text/html")
   public String delete(@PathVariable("id") Long id, @RequestParam(value = "page", required = false) Integer page,
         @RequestParam(value = "size", required = false) Integer size, Model uiModel) {
      CommonItems commonItems = commonItemsService.findCommonItems(id);
      commonItemsService.deleteCommonItems(commonItems);
      uiModel.asMap().clear();
      uiModel.addAttribute("page", (page == null) ? "1" : page.toString());
      uiModel.addAttribute("size", (size == null) ? "10" : size.toString());
      return "redirect:/commonitemses";
   }

   void populateEditForm(Model uiModel, CommonItems commonItems) {
      uiModel.addAttribute("commonItems", commonItems);
      List<CommonItems> commonItemses = commonItemsService.findAllCommonItemses();
      commonItemses.add(0, new CommonItems());
      uiModel.addAttribute("commonitemses", commonItemses);
      uiModel.addAttribute("itemTypes", Arrays.asList(ItemTypes.values()));
   }

   String encodeUrlPathSegment(String pathSegment, HttpServletRequest httpServletRequest) {
      String enc = httpServletRequest.getCharacterEncoding();
      if (enc == null) {
         enc = WebUtils.DEFAULT_CHARACTER_ENCODING;
      }
      try {
         pathSegment = UriUtils.encodePathSegment(pathSegment, enc);
      } catch (UnsupportedEncodingException uee) {
      }
      return pathSegment;
   }
}
